<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */
//用户端登陆
//基本思路：当需要用户登陆方可访问时，跳转至此页。先检测用户环境（微信端、手机端、pc端、小程序端）
//微信端和小程序端统一处理：如果存在$_SESSION['openid']则根据openid来登陆
//微信端如果没能获取到openid，则类比普通手机端处理
//普通手机端和pc端统一处理
//总结精炼：登陆（注册）前不判断，登陆（注册）后，如果session中有openid，则把当前粉丝与登陆的用户绑定
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_SESSION['uid']){
		if($_SESSION['os']=='windows'){
				exi('您已登陆！','error','jy_weishop/member.index/');
		}else{
				exi('您已登陆！','error','core/member.index/');
		}
}
$return['account']=$_SESSION['account'];
$return['extradata']=array();
$setting=uni_setting('register');
$setting['sfyzx']=intval($setting['sfyzx']);
set_html('title','注册会员');
if($_RQ['op']=='get'){
	$return['verify_code']=$_AR['system']['verify_code'];
	$return['title']='注册';
	$setting['sfyzx'] = $setting['sfyzx'] ? $setting['sfyzx'] : '2';
	$setting=uni_setting('register');
	$return['login_type']=intval($setting['login_type']);
	$return['item']=array('setting'=>$setting['sfyzx'],'login_type'=>$return['login_type']==2?0:$return['login_type']);
	if($_SESSION['os']=='windows'){
		$return['session_id']=session_id();
	}
	$_SESSION['forward_url']='jy_weishop/member.index//current_plugin=jy_weishop'; 
	$return['item']['forward_url']=base64_encode($_SESSION['forward_url']);
	exi($return);
}
if($_RQ['op']=='sendcode'){
	$post=get_postdata();
	$mobile=$post['mobile'];
	$code=rand(1000,9999);
	$_SESSION['smscode']=$code;
	$content=array('code'=>$code);
	$setting=uni_setting('member_setting');
	$mid=$setting['sms_mb']['login'];
	$result=cfc('jy_alisms')->send($mobile,$content,$mid);
	if($result){
		exi('success');
	}else{
		exi('error');
	}

}
if($_RQ['op']=='register'){
	if($_RQ['postdata']){
		//注册
		$post=get_postdata();
		if($post['login_type']!=1){
			$post['name'] = trim($post['name']);
			if(!preg_match('/^[\x{4e00}-\x{9fa5}a-z\d_\.]{3,15}$/iu', $post['name'])){
				exi('必须输入用户名，格式为 3-15 位字符，可以包括汉字、字母（不区分大小写）、数字、下划线和句点。','error');
			}
			if(pdo_getcolumn('core_members',array('username' => $post['name']),'id')){
				exi('非常抱歉，此用户名已经被注册，你需要更换注册名称！','error');
			}
			if($post['name'] == $post['password']){
				exi('账号跟密码不能相同！','error');
			}
			if($post['password'] !=$post['password1']) {
				exi('两次密码输入不一致！','error');
			}
			if(strlen($post['password']) < 8) {
				exi('必须输入密码，且密码长度不得低于8位。','error');
			}
		}else{
			//手机验证码注册
			if(!$post['mobile']){
				exi('请输入手机号码！','error');
			}
			if($post['mobile'] == $post['password']){
				exi('手机号跟密码不能相同！','error');
			}
			if(!$post['password'] || !$post['password1']){
				exi('请输入密码！','error');
			}
			if($post['password'] != $post['password1']){
				exi('两次输入密码不一致！','error');
			}
			if(strlen($post['password']) < 8) {
				exi('必须输入密码，且密码长度不得低于8位。','error');
			}
			if(!check_sms_code($post['code'])){
				exi('手机验证码错误！','error');
			}
			$uid=pdo_getcolumn('core_members',array('mobile'=>trim($post['mobile']),'uniacid'=>$_SESSION['uniacid']),'id');
			if($uid){
				exi('手机号已存在，请更换!','error');
			}
		}
		if($post['check']!=1){
			exi('请先阅读协议！','error');
		}
		$insert=array('mobile'=>trim($post['mobile']),'salt'=>random(10));
		$insert['password']=get_password(trim($post['password']),$insert['salt']);
		$insert['username']=$post['name'];
		$insert['createtime']=TIMESTAMP;
		$insert['from_os']=get_os();
		$insert['uniacid']=$_SESSION['uniacid'];
		pdo_insert('core_members',$insert);
		$uid=pdo_insertid();
		$_SESSION['uid']=$uid;
		$_SESSION['member']=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','nickname','avatar'));
		if($_SESSION['openid']){
			$fans=pdo_get('core_fans',array('openid'=>$_SESSION['openid']),array('unionid','nickname','avatar'));
			$update['nickname']=$fans['nickname'];
			$update['avatar']=$fans['avatar'];
			if($fans['unionid']){
				$update['unionid']=$fans['unionid'];
			}
			pdo_update('core_fans',array('mid'=>$uid),array('openid'=>$_SESSION['openid']));
			pdo_update('core_members',$update,array('id'=>$_SESSION['uid']));
		}
		if($_SESSION['forward_url']){
			exi('注册成功！','',$_SESSION['forward_url']);
		}else{
			exi('注册成功！','success','jy_weishop/index/');
		}
	
	}
	$return['type']=1;
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='qq_login'){
	if(!$_AR['system']['qq_login']){
		exi('系统暂不支持QQ登陆！','error','./index.php');
	}
	if($_RQ['step']=='back'){
		if($_RQ['openid']){
			//获得openid，为用户登陆
			$openid=trim($_RQ['openid']);
			$uniacid=pdo_getcolumn('core_open_user',array('open_id'=>'1002','open_uid'=>$openid),'uniacid');
			if(!$uniacid){
				$setting=core_setting('zhuce');
				if(!$setting['open']){
					exi('当前QQ尚未绑定本站账号！系统自动注册失败:本站暂未开启注册！','error','./index.php');
				}
				do{
					$username='qq_'.random(10);
					$check=pdo_getcolumn('core_users',array('name'=>$username),'id');
				}while($check);
				$member=array('name'=>$username);
				$uniacid=cfc('user')->create_user($member);
				$user=array(
					'uniacid'=>$uniacid,
					'open_id'=>'1002',
					'open_uid'=>$openid,
				);
				pdo_insert('core_open_user',$user);
			}
			//登陆用户
			cfc('user')->login($uniacid);
			//跳转首页
			$_SESSION['access']=array();
			exi('授权登陆成功！','','./index.php');
		}else{
			$_SESSION['access']=array();
			exi('授权登录失败！','','./index.php');
		}
		
	}
	//微信授权登陆
	$back_url=SITEROOT.'custom/index.php?plugin=core&action=member.login&op=qq_login&step=back';
	$url=cloud_request('cloud.login.qq',array('back_url'=>$back_url));
	exi('','',$url);
}
?>